package com.example.myanimation;

import android.animation.Keyframe;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Spinner;

import com.example.myanimation.MyEvalutor.CharEvaluator;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PropertyActivity extends AppCompatActivity {

    @BindView(R.id.spinner)
    Spinner spinner;
    @BindView(R.id.propert_bt)
    Button propertBt;
    @BindView(R.id.mytext)
    MyTextView mytext;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_property);
        ButterKnife.bind(this);
        initEvent();
    }

    private void initEvent() {

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 1:
                        ofIntAndofFloat();
                        break;
                    case 2:
                        myTextViewChange();
                        break;
                    case 3:
                        keyFrame();
                        break;
                    case 4:
                        break;
                    case 5:
                        break;
                    case 6:
                        break;
                    case 7:
                        break;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }

        });
    }

    private void keyFrame() {
        WindowManager manager = this.getWindowManager();
        DisplayMetrics outMetrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(outMetrics);
        int width = outMetrics.widthPixels;
        float height = outMetrics.heightPixels;

        float y = propertBt.getY();

        height = height - y - propertBt.getHeight();

        Keyframe kf1 = Keyframe.ofFloat(0.0f, 0f);
        Keyframe kf2 = Keyframe.ofFloat(0.4f, (float)(height/6));
        Keyframe kf3 = Keyframe.ofFloat(0.6f, (float)(height/4));
        Keyframe kf4 = Keyframe.ofFloat(0.8f, height);
        Keyframe kf5 = Keyframe.ofFloat(1.0f, 0f);

        PropertyValuesHolder p = PropertyValuesHolder.ofKeyframe("translationY", kf1, kf2, kf3, kf4, kf5);
        ObjectAnimator objectAnimation = ObjectAnimator.ofPropertyValuesHolder(propertBt, p);
        objectAnimation.setDuration(3000);
        objectAnimation.start();

//        Keyframe frame0 = Keyframe.ofInt(0f, 0);
//        Keyframe frame1 = Keyframe.ofInt(0.1f, 20);
//        Keyframe frame2 = Keyframe.ofInt(0.2f, 40);
//        Keyframe frame3 = Keyframe.ofInt(0.3f, 60);
//        Keyframe frame4 = Keyframe.ofInt(0.4f, 80);
//        Keyframe frame5 = Keyframe.ofInt(0.5f, 100);
//        Keyframe frame6 = Keyframe.ofInt(0.6f, 120);
//        Keyframe frame7 = Keyframe.ofInt(0.7f, 140);
//        Keyframe frame8 = Keyframe.ofInt(1.0f, 160);
////        Keyframe frame0 = Keyframe.ofFloat(0f, 0f);
////        Keyframe frame1 = Keyframe.ofFloat(0.1f, 20f);
////        Keyframe frame2 = Keyframe.ofFloat(0.2f, 40f);
////        Keyframe frame3 = Keyframe.ofFloat(0.3f, 60f);
////        Keyframe frame4 = Keyframe.ofFloat(0.4f, 80f);
////        Keyframe frame5 = Keyframe.ofFloat(0.5f, 100f);
////        Keyframe frame6 = Keyframe.ofFloat(0.6f, 120f);
//        Keyframe frame7 = Keyframe.ofFloat(0.7f, 140f);
////        Keyframe frame8 = Keyframe.ofFloat(1.0f, 160f);
//        PropertyValuesHolder frameHolder1 = PropertyValuesHolder.ofKeyframe("rotation", frame0, frame1, frame2, frame3, frame4,frame5, frame6, frame7, frame8);
//        ObjectAnimator objectAnimation = ObjectAnimator.ofPropertyValuesHolder(propertBt, frameHolder1);
//        objectAnimation.setDuration(3000);
//        objectAnimation.start();
//

    }

    private void myTextViewChange() {
        PropertyValuesHolder propertyOfObject = PropertyValuesHolder.ofObject("CharText", new CharEvaluator(), new Character('a'), new Character('z'));
        PropertyValuesHolder propertyOfFloat = PropertyValuesHolder.ofFloat("rotation", 60, -60, 40, -40, 20, -20, 0);
        PropertyValuesHolder propertyOfInt = PropertyValuesHolder.ofInt("backgroundColor", 0xffffffff, 0xffff00ff, 0xffffff00, 0xffffffff, 0xff000000);
        ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(mytext, propertyOfObject, propertyOfFloat, propertyOfInt);
        objectAnimator.setDuration(3000);
        objectAnimator.start();
    }

    private void ofIntAndofFloat() {
        PropertyValuesHolder propertyOfFloat = PropertyValuesHolder.ofFloat("rotation", 60, -60, 40, -40, 20, -20, 0);
        PropertyValuesHolder propertyOfInt = PropertyValuesHolder.ofInt("backgroundColor", 0xffffffff, 0xffff00ff, 0xffffff00, 0xffffff00);
        ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(propertBt, propertyOfFloat, propertyOfInt);
        objectAnimator.setDuration(3000);
        objectAnimator.start();
    }
}
